//
//  AppDelegate.swift
//  Weibo
//
//  Created by 张小东 on 16/1/10.
//  Copyright © 2016年 Mr.Zhang. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // 设置全局颜色
        setBarColor()
        // 注册通知
        registerNotification()
        
        print(UserAccount.loadUserAccount())
        
        // 实例化一个window
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        // 设置window的背景颜色
        window?.backgroundColor = UIColor.whiteColor()
        
        window?.rootViewController = defaultRootViewController()
        
        //
        window?.makeKeyAndVisible()
    
        return true
    }

    // 注册通知
    private func registerNotification() {
        //object  = nil 时 发出的是一条广播
        //object != 时  发出的是一条 一对一 的通知
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "choseRootVC:", name: WBSwitchRootVC, object: nil)
        
    }
    // 实现通知方法
    @objc private func choseRootVC(n: NSNotification) {
        print(n)
        window?.rootViewController = (n.object == nil ? MainViewController() : WelcomeViewController())
    }
    
    // 根据用户是否登录 指定默认的根视图控制器
    private func defaultRootViewController() ->UIViewController {
        return UserAccountModel().userLogin ? WelcomeViewController() : MainViewController()
    }
    
    
    private func setBarColor() {
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
        UITabBar.appearance().tintColor = UIColor.orangeColor()
    }
   
}

